<?php
/*
* IKPHP爱客网 安装程序 @copyright (c) 2012-3000 IKPHP All Rights Reserved @author 小麦
* @Email:ikphp@sina.cn
* @小麦 关注 2014年8月9日12:11:41
*/
namespace IKPHP\Apps\Space\Models;

use IKPHP\Common\Models\BaseModel;
use IKPHP\Common\Models\User;

class UserFollow extends BaseModel {
	
	public function getSource() {
		return IK . "user_follow";
	}
	
	/**
	 * 判断我是否已经关注过他
	 * @param  integer $userid 我的UID
	 * @param  integer $userid_follow 对方的ID
	 * @return result
	 */
	public static function isFollow($userid, $userid_follow) {
		$result = self::count("userid = '$userid' and userid_follow ='$userid_follow'");
		if ($result) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * userid 查询我关注的用户
	 * @param  integer $userid 我的UID
	 * @param  integer $limit  查询条数
	 * @return result
	 */	
	public static function getfollow_user($userid, $limit){
		$where = array("userid='$userid'",'order'=>'addtime DESC', 'limit'=> $limit);
		$followUsers = self::find($where);
		
		if($followUsers){
			foreach($followUsers as $item){
				$oneuser =  User::getOneUser($item->userid_follow);
				if($oneuser){
					$result[] = $oneuser;
				}
			}
			return $result;	
		}
		
		return null;
	}
	/**
	 * userid 查询谁关注了我
	 * @param  integer $userid_follow 我的UID
	 * @param  integer $limit  查询条数
	 * @return result
	 */	
	public static function getUserFollow($userid_follow,$limit = 10){
		$where = array("userid_follow='$userid_follow'",'order'=>'addtime DESC', 'limit'=> $limit);
		$arrUser = self::find($where);	
		if($arrUser){
			foreach($arrUser as $key=>$item){
				$result[$key] =  User::getOneUser($item->userid);
			}
			return $result;	
		}
		return null;
	}	
	/**
	 * userid   关注某用户
	 * @param  integer $userid 我的UID
	 * @param  integer $userid_follow  要关注的用户UID
	 * @return result
	 */
	public static function follow_user($userid, $userid_follow){
		$data = array (
				'userid' => $userid,
				'userid_follow' => $userid_follow,
				'addtime'=>time(),
		);		
		$mod = new UserFollow(); 
		$user_mod = new User();
		if (false !== $mod->create ( $data )) {
			//更新关注数
			$res1 = $user_mod->findFirst(array("userid = '$userid'"));//关注数
			$res2 = $user_mod->findFirst(array("userid = '$userid_follow'"));//被关注数
			if($res1 && $res2){
				$res1->update(array('count_follow'=>$res1->count_follow+1));
				$res2->update(array('count_followed'=>$res2->count_followed+1));
				return true;
			}
		}
		return false;
	}
	/**
	 * userid   关注某用户
	 * @param  integer $userid 我的UID
	 * @param  integer $userid_follow  要关注的用户UID
	 * @return result
	 */
	public static function unfollow_user($userid, $userid_follow){
		$where = array ("userid = '$userid' and userid_follow = '$userid_follow'");
		$result = self::findFirst($where);
		if($result){
			if($result->delete()){
				$user = new User();
				$user_1 = $user->findFirst("userid = '$userid'");
				$user_1->count_follow = (int)$user_1->count_follow-1;
				$user_1->save();//关注数
				$user_2 = $user->findFirst("userid = '$userid_follow'");
				$user_2->count_followed = (int)$user_2->count_followed-1;
				$user_2->save();//被关注数
				return true;
			}
		}
		return false;
	}		
}
